package com.interesting.client.UI.commonActivity;

import java.io.File;
import java.lang.reflect.Field;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ZoomButtonsController;

import com.interesting.R;
import com.interesting.client.application.TradeApplication;
import com.interesting.client.support.file.CheatSheet;
import com.interesting.client.support.file.FileDownloaderHttpHelper;
import com.interesting.client.support.file.FileLocationMethod;
import com.interesting.client.support.file.FileManager;
import com.interesting.client.support.file.ImageTool;
import com.interesting.client.widget.CircleProgressView;

public class BrowserBigPicActivity extends Activity {

	private PicSimpleBitmapWorkerTask task;
	private PicSaveTask saveTask;
	private String path;
	private String msg;
	private Layout layout;
	private boolean hd;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TradeApplication.getInstance().setActivity(this);
		getWindow().setBackgroundDrawable(new ColorDrawable(0));
		setContentView(R.layout.browserbigpicactivity_layout);
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.width = getResources().getDimensionPixelOffset(R.dimen.browser_pic_activity_width);
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

		layout = new Layout();
		layout.refresh = (ImageButton) findViewById(R.id.ib_refresh);
		layout.share = (ImageButton) findViewById(R.id.ib_share);
		layout.save = (ImageButton) findViewById(R.id.ib_save);
		layout.pb = (CircleProgressView) findViewById(R.id.pb);

		layout.webView = (WebView) findViewById(R.id.iv);

		layout.webView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
		layout.webView.getSettings().setJavaScriptEnabled(true);
		layout.webView.getSettings().setUseWideViewPort(true);
		layout.webView.getSettings().setLoadWithOverviewMode(true);
		layout.webView.getSettings().setBuiltInZoomControls(true);
		// layout.webView.getSettings().setDisplayZoomControls(false);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			layout.webView.getSettings().setDisplayZoomControls(false);
		} else {
			setZoomControlGone(layout.webView);
		}

		layout.webView.setVerticalScrollBarEnabled(false);
		layout.webView.setHorizontalScrollBarEnabled(false);
		layout.webView.setOnTouchListener(new View.OnTouchListener() {
			boolean mPressed;
			boolean mClose;
			CheckForSinglePress mPendingCheckForSinglePress = new CheckForSinglePress();
			long lastTime = 0;
			float[] location = new float[2];

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getActionMasked()) {
				case MotionEvent.ACTION_DOWN:
					mPressed = true;
					if (System.currentTimeMillis() - lastTime > ViewConfiguration.getDoubleTapTimeout() + 100) {
						mClose = true;
						new Handler().postDelayed(mPendingCheckForSinglePress,
								ViewConfiguration.getDoubleTapTimeout() + 100);
					} else {
						mClose = false;
					}
					lastTime = System.currentTimeMillis();

					location[0] = event.getRawX();
					location[1] = event.getRawY();

					break;
				case MotionEvent.ACTION_UP:
					mPressed = false;
					break;
				case MotionEvent.ACTION_CANCEL:
					mPressed = false;
					break;
				case MotionEvent.ACTION_MOVE:
					float x = event.getRawX();
					float y = event.getRawY();
					if (Math.abs(location[0] - x) > 5.0f && Math.abs(location[1] - y) > 5.0f) {
						mClose = false;
					}
					break;
				}

				return false;
			}

			class CheckForSinglePress implements Runnable {

				public void run() {
					if (!mPressed && mClose)
						finish();
				}

			}
		});

		msg = getIntent().getStringExtra("msg");

		task = new PicSimpleBitmapWorkerTask(hd);
		// task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		task.execute();

		initOnClickListener();

		String largePath = FileManager.getFilePathFromUrl(msg, FileLocationMethod.picture_large);
		// if (new File(largePath).exists()) {
		// layout.hd.setVisibility(View.GONE);
		// }
	}

	private void initOnClickListener() {
		layout.refresh.setOnClickListener(onClickListener);
		// layout.copy.setOnClickListener(onClickListener);
		layout.share.setOnClickListener(onClickListener);
		layout.save.setOnClickListener(onClickListener);
		// layout.hd.setOnClickListener(onClickListener);

		CheatSheet.setup(this, layout.refresh, "refresh");
		// CheatSheet.setup(this, layout.copy, R.string.copy_link_to_clipboard);
		CheatSheet.setup(this, layout.share, "share");
		CheatSheet.setup(this, layout.save, "save_pic_album");
		// CheatSheet.setup(this, layout.hd, R.string.switch_to_ori_pic);
	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ib_refresh:
				if (task != null) {
					task.cancel(true);
				}

				if (!TextUtils.isEmpty(path)) {
					new File(path).delete();
				}

				task = new PicSimpleBitmapWorkerTask(hd);
				task.execute();
				// task.executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR);

				break;
			case R.id.ib_share:
				Intent sharingIntent = new Intent(Intent.ACTION_SEND);
				sharingIntent.setType("image/jpeg");
				if (!TextUtils.isEmpty(path)) {
					Uri uri = Uri.fromFile(new File(path));
					sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
					if (UIUtil.isIntentSafe(BrowserBigPicActivity.this, sharingIntent)) {
						startActivity(Intent.createChooser(sharingIntent, "share"));
					}
				}
				break;
			case R.id.ib_save:
				if (task != null && task.getStatus() == AsyncTask.Status.FINISHED) {
					if (saveTask == null) {
						saveTask = new PicSaveTask();
						// saveTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
						saveTask.execute();
					} else if (saveTask.getStatus() == AsyncTask.Status.FINISHED) {
						Toast.makeText(BrowserBigPicActivity.this, "已经保存", Toast.LENGTH_SHORT).show();
					}
				}
				break;

			}
		}
	};

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("msg", msg);
	}

	private class PicSaveTask extends AsyncTask<Void, Boolean, Boolean> {

		@Override
		protected Boolean doInBackground(Void... params) {
			return FileManager.saveToPicDir(path);
		}

		@Override
		protected void onPostExecute(Boolean value) {
			super.onPostExecute(value);
			saveTask = null;
			if (value)
				Toast.makeText(BrowserBigPicActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(BrowserBigPicActivity.this, "不能保存", Toast.LENGTH_SHORT).show();
		}

	}

	class PicSimpleBitmapWorkerTask extends AsyncTask<String, Integer, String> {

		FileDownloaderHttpHelper.DownloadListener downloadListener = new FileDownloaderHttpHelper.DownloadListener() {
			@Override
			public void pushProgress(int progress, int max) {
				publishProgress(progress, max);
			}

		};

		boolean hd;

		public PicSimpleBitmapWorkerTask(boolean hd) {
			this.hd = hd;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// pb.setIndeterminate(true);
			layout.pb.setVisibility(View.VISIBLE);
			layout.webView.setVisibility(View.INVISIBLE);
		}

		@Override
		protected String doInBackground(String... dd) {
			if (isCancelled()) {
				return null;
			}

			// TaskCache.waitForMsgDetailPictureDownload(msg, downloadListener);

			// String middlePath = FileManager
			// .getFilePathFromUrl(msg.getBmiddle_pic(),
			// FileLocationMethod.picture_bmiddle);
			String largePath = FileManager.getFilePathFromUrl(msg, FileLocationMethod.picture_large);
			if (new File(largePath).exists()) {
				return largePath;

			} else {
				String data = msg;
				FileLocationMethod method = FileLocationMethod.picture_large;
				return ImageTool.getLargePictureWithoutRoundedCorner(data, downloadListener, method);
			}

		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			int progress = values[0];
			int max = values[1];
			// pb.setIndeterminate(false);
			layout.pb.setMax(max);
			layout.pb.setProgress(progress);
		}

		@Override
		protected void onPostExecute(final String bitmapPath) {

			if (!TextUtils.isEmpty(bitmapPath)) {
				path = bitmapPath;

				File file = new File(bitmapPath);

				String str1 = "file://" + file.getAbsolutePath().replace("/mnt/sdcard/", "/sdcard/");
				String str2 = "<html>\n<head>\n     <style>\n          html,body{background:transparent;margin:0;padding:0;}          *{-webkit-tap-highlight-color:rgba(0, 0, 0, 0);}\n     </style>\n     <script type=\"text/javascript\">\n     var imgUrl = \""
						+ str1
						+ "\";"
						+ "     var objImage = new Image();\n"
						+ "     var realWidth = 0;\n"
						+ "     var realHeight = 0;\n"
						+ "\n"
						+ "     function onLoad() {\n"
						+ "          objImage.onload = function() {\n"
						+ "               realWidth = objImage.width;\n"
						+ "               realHeight = objImage.height;\n"
						+ "\n"
						+ "               document.gagImg.src = imgUrl;\n"
						+ "               onResize();\n"
						+ "          }\n"
						+ "          objImage.src = imgUrl;\n"
						+ "     }\n"
						+ "\n"
						+ "     function onResize() {\n"
						+ "          var scale = 1;\n"
						+ "          var newWidth = document.gagImg.width;\n"
						+ "          if (realWidth > newWidth) {\n"
						+ "               scale = realWidth / newWidth;\n"
						+ "          } else {\n"
						+ "               scale = newWidth / realWidth;\n"
						+ "          }\n"
						+ "\n"
						+ "          hiddenHeight = Math.ceil(30 * scale);\n"
						+ "          document.getElementById('hiddenBar').style.height = hiddenHeight + \"px\";\n"
						+ "          document.getElementById('hiddenBar').style.marginTop = -hiddenHeight + \"px\";\n"
						+ "     }\n"
						+ "     </script>\n"
						+ "</head>\n"
						+ "<body onload=\"onLoad()\" onresize=\"onResize()\" onclick=\"Android.toggleOverlayDisplay();\">\n"
						+ "     <table style=\"width: 100%;height:100%;\">\n"
						+ "          <tr style=\"width: 100%;\">\n"
						+ "               <td valign=\"middle\" align=\"center\" style=\"width: 100%;\">\n"
						+ "                    <div style=\"display:block\">\n"
						+ "                         <img name=\"gagImg\" src=\"\" width=\"100%\" style=\"\" />\n"
						+ "                    </div>\n"
						+ "                    <div id=\"hiddenBar\" style=\"position:absolute; width: 100%; background: transparent;\"></div>\n"
						+ "               </td>\n" + "          </tr>\n" + "     </table>\n" + "</body>\n" + "</html>";
				layout.webView.loadDataWithBaseURL("file:///android_asset/", str2, "text/html", "utf-8", null);
				layout.webView.setVisibility(View.VISIBLE);
				layout.pb.setVisibility(View.INVISIBLE);

			} else {
				layout.pb.setVisibility(View.GONE);
				int[] attrs = new int[] { R.attr.error };
				TypedArray ta = BrowserBigPicActivity.this.obtainStyledAttributes(attrs);
				Drawable drawableFromTheme = ta.getDrawable(0);
				// webView.setImageDrawable(drawableFromTheme);
			}

		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		UIUtil.cancelTasks(task, saveTask);
		layout.webView.loadUrl("about:blank");
		layout.webView.stopLoading();
	}

	private class Layout {
		WebView webView;
		CircleProgressView pb;

		ImageButton refresh;
		// ImageButton copy;
		ImageButton share;
		ImageButton save;
		// ImageButton hd;
	}

	private void setZoomControlGone(View view) {
		Class<?> classType;
		Field field;
		try {
			classType = WebView.class;
			field = classType.getDeclaredField("mZoomButtonsController");
			field.setAccessible(true);
			ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(view);
			mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
			try {
				field.set(view, mZoomButtonsController);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}
}
